package club.janna.concurrency.example.singleton;

import club.janna.concurrency.annotation.ThreadSafe;

/**
 * 单例-枚举
 * 使用枚举实现单例
 * 利用JVM保证枚举类的构造方法只会执行一次，保证线程安全
 * @Auther: guopanbo
 * @Date: 2018/8/1 21:49
 * @Description:
 */
@ThreadSafe
public class SingletonExample6 {

    private SingletonExample6() {}

    public static SingletonExample6 getInstance() {
        return Singleton.SINGLETON.getInstance();
    }

    private enum Singleton {
        SINGLETON;
        private SingletonExample6 instance = null;
        Singleton() {
            instance = new SingletonExample6();
        }

        public SingletonExample6 getInstance() {
            return instance;
        }
    }

    public static void main(String[] args) {
        System.out.println(getInstance().hashCode());
        System.out.println(getInstance().hashCode());
    }
}
